package net.sureon.common.mail;

import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import net.sureon.common.config.SureonConfig;
import net.sureon.common.exception.SureonException;

/**
 * This is the default implementation of the MailCreator interface, it gets the
 * template from the config and replaces the values in it.
 */
public class DefaultMailCreator implements MailCreator {
	private static final String TEMPLATE_TAG_PREFIX = "%";
	private static final String TEMPLATE_TAG_TO = TEMPLATE_TAG_PREFIX + "TO"
			+ TEMPLATE_TAG_PREFIX;
	private String theTemplate;

	/**
	 * This method is call ONLY ONCE by the MailProcessor. This method should be
	 * used to initialize things in this class.
	 * 
	 * @param key
	 *            The Key that identifies the task
	 * @param templateName
	 *            The Template Name
	 * @param replacementMap
	 *            The replacement Map
	 * @param locale
	 *            The Locale of the email
	 * @throws SureonException
	 */
	public void preProcessMail(Object key, String templateName, MailData data,
			Locale locale) throws SureonException {
		Map replacementMap = data.getReplacementMap();
		theTemplate = SureonConfig.getInstance().getMailConfig().getTemplate(
				templateName, locale);
		if (data.getSubject() == null) {
			String subject = SureonConfig.getInstance().getMailConfig()
					.getSubject(templateName, locale);
			String replacedSubject = setupTemplate(replacementMap, subject);
			data.setSubject(replacedSubject);

		} else {
			setupTemplate(replacementMap, null);
		}
	}

	/**
	 * Creates the mail from the template. This method is called once for every
	 * email address. The generated String object should have the content of the
	 * mail, all the other mail attributes it taken care off by the
	 * MailProcessor
	 * 
	 * @param key
	 *            The Key that identifies the task
	 * @param to
	 *            The email address of the recipient
	 * @param mailData
	 *            The MailData object
	 * @return The content of the mail
	 * @throws SureonException
	 */
	public String createMail(Object key, String to, MailData mailData)
			throws SureonException {
		if (theTemplate != null) {
			String content = theTemplate.replaceAll(TEMPLATE_TAG_TO, to);
			return content;
		}
		return "";
	}

	/**
	 * This method is call ONLY ONCE by the MailProcessor. This method should be
	 * used for clean up in this class
	 * 
	 * @param key
	 *            The Key that identifies the task
	 */
	public void postProcessMail(Object key) throws SureonException {
		// To change body of implemented methods use File | Settings | File
		// Templates.
	}

	protected String setupTemplate(Map attrValues, String subject) {
		Iterator it = attrValues.keySet().iterator();
		while (it.hasNext()) {
			String attr = (String) it.next();
			String attrTag = attr;
			if (!attrTag.startsWith(TEMPLATE_TAG_PREFIX)) {
				attrTag = TEMPLATE_TAG_PREFIX + attr + TEMPLATE_TAG_PREFIX;
			}
			// Now replace
			if (theTemplate != null) {
				theTemplate = theTemplate.replaceAll(attrTag, attrValues.get(
						attr).toString());
			}
			if (subject != null) {
				subject = subject.replaceAll(attrTag, attrValues.get(attr)
						.toString());
			}
		}
		return subject;
	}
}
